﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Text;
using System.IO;

namespace HQWeb.Progect.PhotoManage
{
    /// <summary>
    /// PhotoServer 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    [GenerateScriptType(typeof(Model.Progect.T_PUB_PROJECT))]  //页面注册类
    [GenerateScriptType(typeof(Model.Progect.T_PUB_FILE))]
    [GenerateScriptType(typeof(Model.Progect.T_PUB_FILEATTRIBUTE))]
    public class PhotoServer : System.Web.Services.WebService
    {

        #region 图片设定初始化
        //[WebMethod]
        //public List<Model.Progect.T_PUB_FILE> InitPhoto(string file_name, int state, DateTime begintime, DateTime endtime, int branch_id)
        //{
        //    List<Model.Progect.T_PUB_FILE> modellist = new List<Model.Progect.T_PUB_FILE>();
        //    BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
        //    endtime.AddDays(1);
        //    modellist = bll.GetFileList(file_name, state, begintime, endtime, branch_id);
        //    //int branchid = int.Parse(Session["BRANCH_ID"].ToString());
        //    Model.SysManage.T_PUB_BRANCH branchmodel = new Model.SysManage.T_PUB_BRANCH();
        //    BLL.SysManage.T_PUB_BRANCH branchbll = new BLL.SysManage.T_PUB_BRANCH();
        //    branchmodel = branchbll.GetModel(branch_id);
        //    string ftp = "ftp://" + branchmodel.NAS_IP + "/";
        //    foreach (Model.Progect.T_PUB_FILE item in modellist)
        //    {
        //        item.URLOriginal = ftp + item.URLOriginal;
        //        //item.UPTIME = Convert.ToDateTime(Convert.ToDateTime(item.UPTIME).ToShortDateString());
        //        //item.UPTIME.ToShortDateString()
        //    }
        //    return modellist;
        //}
        #endregion

        #region 获取文件属性
        [WebMethod(Description = "获取文件属性")]
        public Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute(string fileid)
        {
            Model.Progect.T_PUB_FILEATTRIBUTE model = new Model.Progect.T_PUB_FILEATTRIBUTE();
            BLL.Progect.T_PUB_FILEATTRIBUTE bll = new BLL.Progect.T_PUB_FILEATTRIBUTE();
            model = bll.GetModel(fileid);
            return model;
        }
        #endregion

        #region 根据文件编号获取文件
        [WebMethod]
        public Model.Progect.T_PUB_FILE GetFile(string fileid)
        {
            Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
            BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
            model = bll.GetModel(fileid);
            return model;
        }
        #endregion

        #region 文件添加属性和添加项目
        [WebMethod(EnableSession = true)]
        public string AddProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute, IList<Model.Progect.T_PUB_PROJECTFILE> projectlist)
        {
            string resout = "";
            if (Session["USER_ID"] != null)
            {
                FileModel.ISFLAG = true;
                FileModel.GETTIME = DateTime.Now;
                FileModel.UPDATEUSERID = Convert.ToInt32(Session["USER_ID"]);
                FileModel.UPDATEUSERNAME = Session["USER_NAME"].ToString();
                Session["FILE_ID"] = FileModel.FILE_ID;
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                try
                {
                    bll.AddFileProject(FileModel, FileAttribute, projectlist);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加文件" + FileModel.FILE_TITEL + " 的属性和项目", DateTime.Now);
                    //if (Session["UPLOAD"] != null && Session["UPLOAD"].ToString() == "2")
                    //{
                    //    //同步属性
                    //    Session.Remove("UPLOAD");
                    string fileguid = FileModel.FILE_ID.Substring(0, 36).ToString();
                    int branchid = Convert.ToInt32(FileModel.FILE_ID.Substring(37, FileModel.FILE_ID.Length - 37).ToString());
                    bll.SynchronizaionToAllBranch(fileguid, true, true, branchid);
                    //}
                    UpdateCaceData();
                }
                catch (Exception ex)
                {

                    resout = ex.Message;
                }
            }
            else
            {
                resout = "SessionTimeout";
            }
            return resout;
        }
        #endregion

        #region 文件添加属性
        [WebMethod(EnableSession = true)]
        public string Add(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute)
        {
            string resout = "";
            if (Session["USER_ID"] != null)
            {
                Session["FILE_ID"] = FileModel.FILE_ID;
                FileModel.GETTIME = DateTime.Now;
                FileModel.UPDATEUSERID = Convert.ToInt32(Session["USER_ID"]);
                FileModel.UPDATEUSERNAME = Session["USER_NAME"].ToString();
                FileModel.ISFLAG = true;
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                try
                {
                    bll.AddFileProject(FileModel, FileAttribute);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加文件" + FileModel.FILE_TITEL + " 的属性", DateTime.Now);
                    //if (Session["UPLOAD"] != null && Session["UPLOAD"].ToString() == "2")
                    //{
                    //    //同步属性
                    //    Session.Remove("UPLOAD");
                    string fileguid = FileModel.FILE_ID.Substring(0, 36).ToString();
                    int branchid = Convert.ToInt32(FileModel.FILE_ID.Substring(37, FileModel.FILE_ID.Length - 37).ToString());
                    bll.SynchronizaionToAllBranch(fileguid, true, false, branchid);
                    //}
                    UpdateCaceData();
                }
                catch (Exception ex)
                {

                    resout = ex.Message;
                }
            }
            else
            {
                resout = "SessionTimeout";
            }
            return resout;
        }
        #endregion

        #region 文件修改属性和添加和修改项目
        [WebMethod(EnableSession = true)]
        public string EditProject(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute, IList<Model.Progect.T_PUB_PROJECTFILE> projectlist)
        {
            string resout = "";
            if (Session["USER_ID"] != null)
            {
                Session["FILE_ID"] = FileModel.FILE_ID;
                FileModel.GETTIME = DateTime.Now;
                FileModel.UPDATEUSERID = Convert.ToInt32(Session["USER_ID"]);
                FileModel.UPDATEUSERNAME = Session["USER_NAME"].ToString();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                IList<Model.Progect.T_PUB_PROJECTFILE> addList = new List<Model.Progect.T_PUB_PROJECTFILE>();
                IList<Model.Progect.T_PUB_PROJECTFILE> editList = new List<Model.Progect.T_PUB_PROJECTFILE>();
                foreach (Model.Progect.T_PUB_PROJECTFILE item in projectlist)
                {
                    if (item.ID == 0)
                    {
                        addList.Add(item);
                    }
                    else
                    {
                        editList.Add(item);
                    }
                }
                try
                {
                    FileModel.ISFLAG = true;
                    bll.EditFileProject(FileModel, FileAttribute, addList, editList);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "修改文件" + FileModel.FILE_TITEL + " 的属性和项目", DateTime.Now);
                    UpdateCaceData();
                }
                catch (Exception ex)
                {

                    resout = ex.Message;
                }
            }
            else
            {
                resout = "SessionTimeout";
            }
            return resout;
        }
        #endregion

        #region 文件修改属性
        [WebMethod(EnableSession = true)]
        public string Edit(Model.Progect.T_PUB_FILE FileModel, Model.Progect.T_PUB_FILEATTRIBUTE FileAttribute)
        {
            string resout = "";
            if (Session["USER_ID"] != null)
            {
                Session["FILE_ID"] = FileModel.FILE_ID;
                FileModel.GETTIME = DateTime.Now;
                FileModel.UPDATEUSERID = Convert.ToInt32(Session["USER_ID"]);
                FileModel.UPDATEUSERNAME = Session["USER_NAME"].ToString();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                //IList<Model.Progect.T_PUB_PROJECTFILE> addList = new List<Model.Progect.T_PUB_PROJECTFILE>();
                //IList<Model.Progect.T_PUB_PROJECTFILE> editList = new List<Model.Progect.T_PUB_PROJECTFILE>();
                try
                {
                    FileModel.ISFLAG = true;
                    bll.EditFileProject(FileModel, FileAttribute);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "修改文件" + FileModel.FILE_TITEL + " 的属性", DateTime.Now);
                    UpdateCaceData();
                }
                catch (Exception ex)
                {

                    resout = ex.Message;
                }
            }
            else
            {
                resout = "SessionTimeout";
            }
            return resout;
        }
        #endregion

        #region 删除文件项目
        [WebMethod(EnableSession = true)]
        public string DelFileProject(string strID)
        {
            string strResout = "";
            if (Session["USER_ID"] != null)
            {
                BLL.Progect.T_PUB_PROJECTFILE bll = new BLL.Progect.T_PUB_PROJECTFILE();
                int temp;
                int.TryParse(strID, out temp);
                try
                {
                    bll.Delete(temp);
                    Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除文件项目" + strID, DateTime.Now);
                    UpdateCaceData();
                }
                catch (Exception ex)
                {

                    strResout = ex.Message;
                }
            }
            else
            {
                strResout = "SessionTimeout";
            }
            return strResout;
        }
        #endregion

        #region 初始化文件项目
        [WebMethod(EnableSession = true)]
        public string FileProject(string strID)
        {
            BLL.Progect.T_PUB_PROJECTFILE bll = new BLL.Progect.T_PUB_PROJECTFILE();
            DataTable modelList = bll.GetProjectList(strID).Tables[0];
            int count = modelList.Rows.Count;
            StringBuilder strHtml = new StringBuilder();
            StringBuilder strProjectIDList = new StringBuilder();
            strProjectIDList.Append(",");
            string strEID;
            string strProjectId;
            string strProjectName;
            string strPrice;
            string strBudget;
            string strType;
            string strRemark;
            for (int i = 0; i < count; i++)
            {
                strEID = modelList.Rows[i]["ID"].ToString();
                strProjectId = modelList.Rows[i]["PROJECT_ID"].ToString();
                strProjectIDList.Append(strProjectId + ',');
                strProjectName = modelList.Rows[i]["PROJECT_NAME"].ToString();
                strPrice = modelList.Rows[i]["PRICE"].ToString();
                strBudget = modelList.Rows[i]["BUDGET"].ToString();
                strType = modelList.Rows[i]["PROJECTTYPE_ID"].ToString();
                strRemark = modelList.Rows[i]["REMARK"].ToString();
                strHtml.Append(" <table width=\"98%\" id=\"table" + i + "\">");
                strHtml.Append(" <tr>");
                strHtml.Append(" <td align=\"right\" style=\"width:20%\">");
                strHtml.Append("服务项目名称：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:20%\">");
                strHtml.Append(strProjectName);
                strHtml.Append("</td>");
                strHtml.Append(" <td align=\"right\" style=\"width:20%\">");
                strHtml.Append("价 格：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" style=\"width:20%\">");
                strHtml.Append(strPrice);
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr>");
                strHtml.Append(" <td align=\"right\" style=\"width:20%\">");
                strHtml.Append("说 明：");
                strHtml.Append("</td>");
                strHtml.Append("<td align=\"left\" colspan=\"3\">");
                strHtml.Append(strRemark);
                strHtml.Append("</td>");
                strHtml.Append("</tr>");
                strHtml.Append("<tr align=\"left\">");
                strHtml.Append("<td colspan=\"4\">");
                if (Convert.ToBoolean(Session["FILE_EDIT"]) == true)
                {
                    strHtml.Append("<input type=\"button\" id=\"del" + i + "\" value=\"删除该元素\"  onclick=\"DeleteNode('table" + i + "')\"/>");
                }
                strHtml.Append("<input type=\"text\" style=\"display: none\" value=\"" + strProjectId + "\" id=\"p" + i + "\" />");
                strHtml.Append(" <input type=\"text\" id=\"e" + i + "\" style=\"display: none\" value=\"" + strEID + "\" />");
                strHtml.Append(" <input type=\"text\" id=\"b" + i + "\" style=\"display: none\" value=\"" + strBudget + "\" />");
                strHtml.Append(" <input type=\"text\" id=\"t" + i + "\" style=\"display: none\" value=\"" + strType + "\" />");
                strHtml.Append("</td>");
                strHtml.Append(" </tr>");
                strHtml.Append(" </table>");
            }
            StringBuilder strRT = new StringBuilder();
            strRT.Append(count + "@" + strProjectIDList + "@" + strHtml.ToString());
            return strRT.ToString();
        }
        #endregion

        #region 删除文件
        [WebMethod(EnableSession = true)]
        public string DeleteFile(string strID, string strSecurity, string flag)
        {
            string strResout = "";
            if (Session["USER_ID"] != null)
            {
                Model.SysManage.T_PUB_SECURITY model = new Model.SysManage.T_PUB_SECURITY();
                BLL.SysManage.T_PUB_SECURITY bll = new BLL.SysManage.T_PUB_SECURITY();
                BLL.Progect.T_PUB_FILE filebll = new BLL.Progect.T_PUB_FILE();
                Model.Progect.T_PUB_FILE filemodel = new Model.Progect.T_PUB_FILE();
                filemodel = filebll.GetModel(strID + "-" + Session["BRANCH_ID"].ToString());
                model = bll.GetModel();
                if (strSecurity.Equals(model.SECURITY))
                {
                    int diffMinute = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Minute"]);
                    if (DateTime.Now < Convert.ToDateTime(model.PRODUCE_TIME).AddMinutes(diffMinute))
                    {
                        try
                        {
                            if (filebll.DeleteFile(strID.Substring(0, 36), Convert.ToInt32(Session["USER_ID"]), Session["USER_NAME"].ToString()))
                            {
                                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "删除文件" + strID, DateTime.Now);
                                UpdateCaceData();
                                string HeadNasIP = System.Configuration.ConfigurationManager.AppSettings["HeadNASIP"];
                                string HeadUserName = System.Configuration.ConfigurationManager.AppSettings["HeadUserName"];
                                string HeadPassword = System.Configuration.ConfigurationManager.AppSettings["HeadPassword"];
                                string ImageURL = System.Configuration.ConfigurationManager.AppSettings["ImageURL"];
                                string VideoURL = System.Configuration.ConfigurationManager.AppSettings["VideoURL"];
                                FTPClient headftp = new FTPClient(HeadNasIP, "", HeadUserName, HeadPassword, 21);//总店FTP
                                if (flag == "1")
                                {
                                    headftp.ChDir(VideoURL);
                                }
                                else if (flag == "0")
                                {
                                    headftp.ChDir(ImageURL);
                                }
                                headftp.Delete(filemodel.FILE_NAME);
                                //获得图片的服务器路径 
                                string delFile = Server.MapPath("./").Substring(0, 16) + filemodel.URLThumbnail.Replace("//", "\\");
                                //删除图片 
                                if (File.Exists(delFile))
                                {
                                    File.Delete(delFile);
                                }
                                strResout = "";
                            }
                            else
                            {
                                strResout = "删除文件错误！请您重试";
                            }
                        }
                        catch (Exception ex)
                        {

                            strResout = ex.Message;
                        }
                    }
                    else
                    {
                        strResout = "密码已经过期，请重新获取！";
                    }
                }
                else
                {
                    strResout = "输入的密码有误！";
                }
            }
            else
            {
                strResout = "SessionTimeout";
            }
            return strResout;
        }
        #endregion

        #region 隐藏文件
        [WebMethod(EnableSession=true)]
        public string HiddenFile(string strID)
        {
            string strResout = "";
            if (Session["USER_ID"] != null)
            {
                Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                model = bll.GetModel(strID + "-" + Session["BRANCH_ID"].ToString());
                if (model != null)
                {
                    model.PROJECTREAMRK = "0";
                    if (bll.Update(model))
                    {
                        Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "隐藏文件" + strID, DateTime.Now);
                        UpdateCaceData();
                    }
                    else
                    {
                        strResout = "隐藏文件失败！";
                    }
                }
                else
                {
                    strResout = "不存在该文件！";
                }
            }
            else
            {
                strResout = "SessionTimeout";
            }
            return strResout;         
        }
        #endregion

        #region  更新缓存数据
        private void UpdateCaceData()
        {
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
            //Universal.Caches.InsertCommonInf("Exhibition");
        }
        #endregion
    }
}
